public class isLineSegment implements getLength{
    Point point1;
    Point point2;

    public isLineSegment(Point a, Point b) {
        this.point1 = a;
        this.point2 = b;
    }

    //获取线段长度接口实现
    @Override
    public double getLength() {
        double length;
        double height;
        double result;

        length = point1.getX()-point2.getX();
        height = point1.getY()-point2.getY();

        result = Math.sqrt(Math.pow(length,2) + Math.pow(height,2));

        return result;
    }

    //斜率接口实现
    @Override
    public double getSlope() {
        if (point1.getX() == point2.getX()) {
            return 1;                               //1表示斜率无穷大
        } else {
            return (double)((point1.getY()-point2.getY())/(point1.getX()-point2.getX()));

        }
    }

    //输出实现斜率接口
    @Override
    public void Slope() {
        if(getSlope() == 1) {
            if (isOnLine(new Point(0, 0))) {
                System.out.println("Slope: " + getSlope());
            } else {
                System.out.println("该线段的斜率为无穷大！");
            }
        } else {
            System.out.println("Slope: " + getSlope());
        }
    }

    //截距接口实现
    @Override
    public double getIntercept() {
        double minX = Math.min(point1.getX(),point2.getX());
        double maxX = Math.max(point1.getX(),point2.getX());

        if(getSlope() == 1) {
            return 0;                           //0表示没有截距
        } else {
            if (minX > 0 || maxX<0) {
                return 0;
            } else {
                return  point1.getY() - getSlope() * point1.getX();
            }
        }
    }
    //输出截距接口实现
    @Override
    public void Intercept() {
        double minX = Math.min(point1.getX(),point2.getX());
        double maxX = Math.max(point1.getX(),point2.getX());

        if (getIntercept() == 0) {
            if (minX > 0 || maxX<0) {
                System.out.println("该线段没有截距！");
            } else {
                System.out.println(getIntercept());
            }
        } else {
            System.out.println("Intercept: " + getIntercept());
        }
    }

    //    判断点是否在线上，在返回1，不在返回0
//    int onSegement(point p1,point p2,point Q)
//    double maxx,minx,maxy,miny;
//    maxx = p1.x >p2.x ?p1.x :p2.x ;    //矩形的右边长
//    minx = p1.x >p2.x ?p2.x :p1.x ;     //矩形的左边长
//    maxy = p1.y >p2.y ?p1.y :p2.y ;    //矩形的上边长
//    miny = p1.y >p2.y ?p2.y :p1.y ;     //矩形的下边长
//    if( ((Q.x -p1.x )*(p2.y -p1.y) == (p2.x -p1.x) *(Q.y -p1.y)) && ( Q.x >= minx && Q.x <= maxx ) && ( Q.y >= miny && Q.y <= maxy))
//         return 1;
//    else
//        return 0;
    @Override
    public boolean isOnLine(Point p) {
        boolean flag = false;

        double maxX = Math.max(point1.getX(),point2.getX());
        double maxY = Math.max(point1.getY(),point2.getY());
        double minX = Math.min(point1.getX(),point2.getX());
        double minY = Math.min(point1.getY(),point2.getY());

        if ((p.getX()-point1.getX()) * (point1.getY() - point2.getY()) == (p.getY()-point1.getY()) * (point1.getY() - point2.getY()) &&
                (p.getX() >= minX && p.getX() <= maxX) && (p.getY()>=minY && p.getY()<=maxY)){
            return true;
        } else {
            return false;
        }
    }
}
